﻿using MediatR;
using Microsoft.EntityFrameworkCore;
using Traceability.Domain;
using Traceability.ErrorCode;
using Traceability.Infrastructure;

namespace Traceability.App.Applications.Aquaculture
{
    public class AquacultureQueryCommandHandler : IRequestHandler<AquacultureQueryCommand, APIResult<T_Aquaculture>>
    {
        private readonly IBaseRepository<T_Aquaculture> aquacultureRepository;

        public AquacultureQueryCommandHandler(IBaseRepository<T_Aquaculture> aquacultureRepository)
        {
            this.aquacultureRepository = aquacultureRepository;
        }

        public Task<APIResult<T_Aquaculture>> Handle(AquacultureQueryCommand request, CancellationToken cancellationToken)
        {
            APIResult<T_Aquaculture> result = new APIResult<T_Aquaculture>();
            result.Code = APIEnum.ok;
            result.Message = "获取采购";
            result.Data = aquacultureRepository.GetAll().AsNoTracking().FirstOrDefault(x => x.Batch == request.Batch);
            return Task.FromResult(result);
        }
    }
}
